package com.bkgy.modular.yieldReport.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.yieldReport.dto.AosrpeDto;
import com.bkgy.modular.yieldReport.dto.TbDsjActWeightDto;
import com.bkgy.modular.yieldReport.entity.*;
import com.bkgy.modular.yieldReport.mapper.ReportFormsMapper;
import com.bkgy.modular.yieldReport.service.ReportFormsService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version 1.0
 * @ClassName ReportFormsServiceImpl
 * @date 2023/3/31 17:48
 */
@Service
public class ReportFormsServiceImpl extends ServiceImpl<ReportFormsMapper, ReportForms> implements ReportFormsService {
    @Resource
    private ReportFormsMapper reportFormsMapper;

    @Override
    public ResultData reportFormsFindByData(TbDsjActWeightDto dto) {
        Integer pageIndex = dto.getPageIndex();
        Integer pageSize = dto.getPageSize();
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("检查分页参数是否正确", -1);
        }
        List<ReportForms> reportFormsList = reportFormsMapper.findByData((pageIndex - 1) * pageSize, pageSize * pageIndex, dto);
        Integer count = reportFormsMapper.findByCount(dto);
        return ResultData.ResultDataSuccess(reportFormsList, count);
    }

    @Override
    public ResultData reportFormsFindDataSXSD(JSONObject jsonObject) {
        JSONObject condition = jsonObject.getJSONObject("condition");
        if (condition == null) {
            return ResultData.ResultDataFaultSelf("condition参数没有传", null);
        }
        List<ReportFormsSXSD> reportFormsList = reportFormsMapper.findByDataSXSD(condition);

        return ResultData.ResultDataSuccess(reportFormsList);
    }

    @Override
    public ResultData findByStatistics(TbDsjActWeightDto dto) {
        List<ReportForms> byStatistics = reportFormsMapper.findByStatistics(dto);
        return ResultData.ResultDataFaultSelf("操作成功", byStatistics);
    }

    /**
     * 日产量报表业务实现
     *
     * @param dto
     * @return
     */
    @Override
    public ResultData findByDailyProduction(TbDsjActWeightDto dto) {
        List<DailyProduction> byDailyProduction = reportFormsMapper.findByDailyProduction(dto);
        return ResultData.ResultDataFaultSelf("操作成功", byDailyProduction);
    }

    @Override
    public ResultData findByHourlyProduction(TbDsjActWeightDto dto) {
        List<DailyProduction> byHourlyProduction = reportFormsMapper.findByHourlyProduction(dto);
        return ResultData.ResultDataFaultSelf("操作成功", byHourlyProduction);
    }

    /**
     * 单卷生产效率分析 业务实现
     *
     * @param dto
     * @return
     */
    @Override
    public ResultData findAosrpe(AosrpeDto dto) {
        // 接收分页参数
        Integer pageIndex = dto.getPageIndex();
        Integer pageSize = dto.getPageSize();
        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数有误", null);
        }
        List<Aosrpe> aosrpeList = reportFormsMapper.findAosrpe((pageIndex - 1) * pageSize, pageSize * pageIndex, dto);
        Integer aosrpeCount = reportFormsMapper.findAosrpeCount(dto);
        return ResultData.ResultDataSuccess(aosrpeList, aosrpeCount);
    }

    /**
     * 四班生产效率分析业务实现
     *
     * @param dto
     * @return
     */
    @Override
    public ResultData findProductionEfficiencyOfFourShifts(AosrpeDto dto) {
        List<ProductionEfficiencyOfFourShifts> productionEfficiencyOfFourShifts = reportFormsMapper.findProductionEfficiencyOfFourShifts(dto);
        return ResultData.ResultDataFaultSelf("操作成功", productionEfficiencyOfFourShifts);
    }

    /**
     * 四班成材率分析业务实现
     *
     * @param dto
     * @return
     */
    @Override
    public ResultData yieldAnalysis(TbDsjActWeightDto dto) {
        List<YieldReport> yieldReports = reportFormsMapper.yieldAnalysis(dto);
        return ResultData.ResultDataFaultSelf("操作成功", yieldReports);
    }


    /**
     * 单卷生产效率分析导出 业务实现
     *
     * @param dto
     * @return
     */
    @Override
    public void aosrpeExcel(AosrpeDto dto, HttpServletResponse response) {
        // 接收分页参数
        Integer pageCount = dto.getPageCount();
        Integer pageSize = dto.getPageSize();
        String fileNames = dto.getFileNames();

        // 创建表格数据集合
        List<Aosrpe> listExcel = new ArrayList<>();

        // 调用service查询出数据
        List<Aosrpe> data = null;
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = reportFormsMapper.findAosrpe((i - 1) * pageSize, i * pageSize, dto);
            for (Aosrpe item : data) {
                listExcel.add(item);
            }
        }
        List<ExportAosrpe> list = new ArrayList<>();
        for (Aosrpe item : listExcel) {
            ExportAosrpe exportAosrpe = new ExportAosrpe();
            BeanUtils.copyProperties(item, exportAosrpe);
            list.add(exportAosrpe);
        }
        try {
            Map<String, Object> data1 = new HashMap();
            data1.put("data", list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\酸轧单卷生产效率分析.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(), false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames, response, workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResultData findOnDutyProduction(TbDsjActWeightDto dto) {
        List<OnDutyProduction> list = reportFormsMapper.findOnDutyProduction(dto);
        return ResultData.ResultDataFaultSelf("操作成功", list);
    }

    @Override
    public void reportFormsFindByDataExcel(TbDsjActWeightDto dto, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = dto.getPageSize();
        Integer pageCount = dto.getPageCount();
        String fileNames = dto.getFileName();

        // 创建表格数据集合
        List<ReportForms> listExcel = new ArrayList<>();

        // 调用service查询出数据
        List<ReportForms> data = null;
        List<ExportReportForms> list = new ArrayList<>();
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = reportFormsMapper.findByData((i - 1) * pageSize, i * pageSize, dto);
            if (!data.isEmpty() && data.size() > 0) {
                for (ReportForms item : data) {
                    ExportReportForms exportReportForms = new ExportReportForms();
                    BeanUtils.copyProperties(item, exportReportForms);
                    list.add(exportReportForms);
                }
            }
        }
        try {
            Map<String, Object> data1 = new HashMap();
            data1.put("data", list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\折算产量.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(), false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames, response, workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
